﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Collections;

namespace Dunnice.BlogIndex.Entity
{
    /// <summary>
    /// 文章信息.
    /// </summary>
    public class BlogArticleInfo : IEqualityComparer<BlogArticleInfo>
    {
        /// <summary>
        /// 文章Id(数据库使用)
        /// </summary>
        public long Bid { get; set; }

        /// <summary>
        /// 文章标题.
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 文章URL.
        /// </summary>
        public string Url { get; set; }

        /// <summary>
        /// 博客服务器名称.
        /// </summary>
        public string ServerName { get; set; }

        /// <summary>
        /// 博客作者名称.
        /// </summary>
        public string AuthorName { get; set; }

        private string _dateString;
        /// <summary>
        /// 文章发布日期.
        /// </summary>
        public string DateString
        {
            get
            {
                return _dateString;
            }
            set
            {
                _dateString = value;
                DateTime tmp;
                if (DateTime.TryParseExact(_dateString, new string[] { "yyyy/MM/dd", "yyyy-MM-dd HH:mm" }, null, System.Globalization.DateTimeStyles.None, out tmp))
                {
                    PostTime = tmp;
                }
            }
        }

        /// <summary>
        /// 页面的html的名称.
        /// </summary>
        public string HtmlPageName { get; set; }

        /// <summary>
        /// 文章发布日期.
        /// </summary>
        public DateTime PostTime { get; set; }

        /// <summary>
        /// 内容.
        /// </summary>
        public string Content { get; set; }

        public override int GetHashCode()
        {
            MD5 md5 = MD5.Create();
            byte[] by = md5.ComputeHash(Encoding.Default.GetBytes(string.Format("{0}_{1}_{2}_{3}", ServerName, AuthorName, this.Url, PostTime)));

            return BitConverter.ToInt32(by, 0);
        }

        public bool Equals(BlogArticleInfo x, BlogArticleInfo y)
        {
            if (x == null || y == null)
            {
                return false;
            }

            if (x.GetHashCode() == y.GetHashCode())
            {
                return true;
            }

            return false;
        }

        public int GetHashCode(BlogArticleInfo obj)
        {
            return obj.GetHashCode();
        }

        public override string ToString()
        {
            return string.Format("[{0}]:{1}", PostTime.ToString("yy-MM-dd HH:mm"), Title);
        }
    }
}
